<template>
  <div class="dashboard-container">
    <div class="app-container">

      <!-- Permission 权限管理 -->
      <!-- 顶部 -->
      <Toptoolbar>
        <template #search>
          <!-- 一级权限pid为'0' type为1 -->
          <el-button type="primary" @click="addperm('1','0')">添加权限</el-button>
        </template>
      </Toptoolbar>
      <!-- 表格 -->
      <el-card class="box-card">
        <el-table
          :data="tableData"
          row-key="id"
          border
          style="width: 100%"
        >
          <el-table-column
            prop="title"
            label="权限名称"
          />
          <el-table-column
            prop="code"
            label="权限标识"
          />
          <el-table-column
            prop="description"
            label="权限描述"
          />
          <el-table-column
            prop="address"
            label="操作"
          >
            <template v-slot="{row}">
              <el-row type="flex">
                <el-button type="primary" size="small" @click="btnperm('2',row.id)">添加子权限</el-button>
                <el-button type="primary" size="small" icon="el-icon-edit" @click="putprtm(row.id)">编辑</el-button>
                <el-button icon="el-icon-delete" @click="delperm(row.id)" />
              </el-row>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
      <!-- 弹窗 -->
      <addpermission ref="addpermission" :visperm.sync="visperm" @getlist="getlist()" />
    </div>
  </div>
</template>
<script>
import { getpermlist, romoveperm } from '@/api/permission'
import addpermission from './components/addpermission.vue'
// 处理数据
import { processdata } from '@/utils/index'

export default {
  components: { addpermission },
  data() {
    return {
      // 表格数据
      tableData: [],
      permdata: {
        _limit: 50,
        _start: 0
      },
      visperm: false

    }
  },
  async created() {
    this.getlist()
  },
  methods: {
    // 添加一级权限
    addperm(type, pid) {
      this.visperm = true
      // 把固定参数传入父组件
      this.$refs.addpermission.ruleForm = { type: type, pid: pid }
    },
    // 获取权限列表
    async  getlist() {
      const res = await getpermlist(this.permdata)
      this.tableData = processdata(res.data, '0')
    },
    // 二级权限
    btnperm(type, id) {
      this.visperm = true
      const pid = id.toString()
      this.$refs.addpermission.ruleForm = { type: type, pid: pid }
    },
    // 编辑权限
    putprtm(id) {
      this.$refs.addpermission.getpermdetail(id)
      this.visperm = true
    },
    // 删除权限
    delperm(id) {
      this.$confirm('确定要删除该权限吗, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await romoveperm(id)

        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        this.getlist()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.pagination{
  margin-top: 20px;
}
</style>
